/*本模块功能:详情页(老师)
        编写者:杨锡君
        编写时间20180518*/
$(function() {
    var confirm1 = new Confirm;
    var Alert1 = new Alert;

    function formatDate(now) {//处理时间戳
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        if(month<10){month="0"+month;}
        if(date<10){date="0"+date;}
        if(hour<10){hour="0"+hour;}
        if(minute<10){minute="0"+minute;}
        return year + "-" + month + "-" + date + "   " + hour + ":" + minute;
    }
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    function alertContent(questionContent,url) { //问题
        return '<div class="fl headPort" problemId="' + questionContent.problemMap.problemId + '"><p class="fl headPortrait"><img src="' + url+questionContent.problemMap.headIcon+ '"/></p></div>' +
            '<div class="fl message">' +
            '<div>'+
            '<span class="fl identity myQuestionName">'+ questionContent.problemMap.userName +'</span><span class="fl time myQuestionTime">'+ timeObject.changedateTime(questionContent.problemMap.problenTime) +'</span>'+
            '</div>'+
            '<div>' +
            '<span class="fl">问题标题:&ensp;</span><span class="fl">' + unescape(questionContent.problemMap.problemName) + '</span>' +
            '</div>' +
            '<div class="clear"></div>' +
            '<div class="message_content">' +
            '<span class="fl">问题描述:&ensp;</span><span  class="fl">' + unescape(questionContent.problemMap.problemContent) + '</span>' +
            '</div>' +
            '</div>' +
            '<div class="clear"></div>';

    }

    function tabelModel(opt,url) { //回答
        var time = new Date(opt.answerTime);
        var str = "";
        if(opt.isMark == 1) {
            str = '<span class="fr">学生</span>';
        }
        if(opt.roleId == 2) {
            str = '<span class="fr">教师</span>';
        }
        var excuseMark = opt.excuseMark;
        var strs = "";
        if(excuseMark == 0){
            strs = "<a name="+ opt.userName +" id="+ opt.stuorteaId +" class=\"closeSpeak \" href=\"javascript:void(0)\">禁言</a>";
        }else{
            strs="<a class=\"hadSpeak \" href=\"javascript:void(0)\">已禁言</a>"
        }
        return '<div class="funBox answer_list" answerId="' + opt.answerId + '">' +
            '<div class="fl headPort">' +
            '<p class="fl headPortrait"><img src="' + url+opt.headIcon + '" /></p>' +
            '</div>' +
            '<div class="fl message">' +
            '<div>' +
            '<span class="fl identity">' + opt.userName + '&ensp;' + str + '</span><span class="fl time">' + formatDate(time) + '</span>' +strs+
            '</div>' +
            '<div class="clear"></div>' +
            '<div class="answer_list_answer">' +
            '<span style="font-weight: bolder;">回答:&ensp;&ensp;</span><span  class="answer_list_answer_area"><div  class="copy"> ' +unescape(opt.answerContent) + '<div class="clear"></div></div></span><i class="last">展开</i><div class="clear"></div>' +
            '</div>' +
            '</div>' +
            '<div class="clear"></div>' +
            '</div>';

    }
    var index = {
        init: function() {
            this.bind();
        },
        bind: function() {
            var self = this;
            var json = {
                "pageNow": 1,
                "pageSize": 10,
                "isMark":result.paramData.isMark,
                "problemId":result.paramData.problemId
            };
            this.postData(json);
            $(".looktg").off("click").click(function () {
                var json = JSON.stringify({
                    "pageName": $(this).attr("data-url"),
                    "paramData": {
                        "some": result.paramData.some
                    }
                });
                window.location.href = "skipPage?jsonStr=" + json;
            });
        },
        skipEvent:function () {
            $(".closeSpeak").off("click").click(function () {
                var $that = $(this);
                var id = $that.attr("id");
                confirm1.content = "是否对"+ $that.attr("name") +"进行禁言？";
                confirm1.success = function () {
                    var json = JSON.stringify({"excuseMark":1,"list":[id]});
                    $.ajax({
                        url:'excuseProblem',
                        method:'post',
                        async:true,
                        data:{
                            "jsonStr":json
                        },
                        success:function (msg) {
                            msg = JSON.parse(msg);
                            if(msg.result == "success"){
                                Alert1.content = "禁言成功";
                                Alert1.success = function () {
                                    // $that.addClass("hadSpeak");
                                    // $that.html("已禁言");
                                    // $that.off("click");
                                    // $that.removeClass("closeSpeak");
                                    var json = JSON.stringify({
                                        "pageName": "adminCourseInterflowDetail",
                                        "paramData":{
                                            "isMark":0,
                                            "problemId":parseInt(result.paramData.problemId)
                                        }
                                    });
                                    window.location.href = "skipPage?jsonStr=" + json;
                                };
                                Alert1.init();
                                Alert1.show();
                            }else{
                                if (msg.errorMsg != "" && msg.errorMsg != undefined) {
                                    Alert1.content = msg.errorMsg;
                                } else {
                                    Alert1.content = "禁言失败";
                                }
                                Alert1.init();
                                Alert1.show();
                            }
                        }
                    });
                };
                confirm1.init();
                confirm1.show();
            });
        },
        postData: function(jsons) { //获得搜索及列表数据
            var self = this;
            var parameter = jsons;
            $.ajax({
                type: "post",
                url: "getProblemDetail",
                async: true,
                data: {
                    "jsonStr": JSON.stringify(parameter)
                },
                success: function(msg) {
                    msg = JSON.parse(msg);
                    var table = "";
                    var url = msg.imageUrl;
                    var question = alertContent(msg,url);
                    $(".putQuestions").html(question);
                    if(msg.answerList != undefined && msg.answerList != null && msg.answerList.length !=0){
                        $.map(msg.answerList,function (opt) {
                            table += tabelModel(opt,url);
                        });
                        $(".answer_content_list_area").html(table);
                        self.skipEvent();
                    }

                    var sumpage = msg.totalPage;
                    if(sumpage<1){sumpage=1;}

                    var nowpage = 1;
                    var pagenumber = 10;
                    $(".tcdPageCode").createPage({
                        pageCount: sumpage, //总页数
                        current: nowpage, //当前页数
                        pageNumber: pagenumber, //每页显示数量
                        backFn: function(p) {

                            nowpage = p;

                            if(nowpage >= sumpage) {
                                nowpage = sumpage;
                            }
                            var jsonStr = jsons;
                            jsonStr.pageNow = nowpage;
                            loading1.show();
                            $.ajax({
                                type: "post",
                                url: "getProblemDetail",
                                async: true,
                                data: {
                                    "jsonStr": JSON.stringify(parameter)
                                },
                                success: function(msg) {
                                    msg = JSON.parse(msg);
                                    var table = "";
                                    var url = msg.imageUrl;
                                    var question = alertContent(msg,url);
                                    $(".putQuestions").html(question);
                                    if(msg.answerList != undefined && msg.answerList != null && msg.answerList.length !=0){
                                        $.map(msg.answerList,function (opt) {
                                            table += tabelModel(opt,url);
                                        });
                                        $(".answer_content_list_area").html(table);
                                        self.skipEvent();
                                    }
                                },
                                error: function(data, status, xhr) {
                                    var json = JSON.stringify({
                                        "pageName": "error"
                                    });
                                    window.location.href = "skipPage?jsonStr=" + json;
                                }
                            });
                        }
                    });
                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        }
    };
    index.init();

});